const globalIp = getApp().globalData.userIp
var barChart = null;
import * as echarts from '../../../../components/ec-canvas/echarts';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec: {
      onInit: function (canvas) {
        let pixelRatio = 0
           wx.getSystemInfo({
             success: function(res) {
              pixelRatio = res.pixelRatio
             },
             fail: function () {
               pixelRatio = 0
             }
           })
           barChart = echarts.init(canvas, null, {
             width: 500,
             height: 250,
             devicePixelRatio: pixelRatio
           });
           canvas.setChart(barChart);
           return barChart;
         }
   },
    searchResult:false,
    screen:"条件选择",
    screenDown: {screenDown:["显示销售部门","显示销售员"],screen:0},
    screenFlag: false,//控制综合下拉框是否显示
    screenColor: false,
    dataColor: false,
    dataTime:"日期选择",
    dataTime1:"日期选择",
    dataTime2:"截止日期",
    dataFlag: false, //控制综合下拉框是否显示
    isShowAll:false,
    nowTime: getApp().globalData.nowTime,
    projectList:[],
    openId:''
  },
  showAll(){
    if(this.data.isShowAll){
      this.setData({
        isShowAll:false
      })
    }else{
      this.setData({
        isShowAll:true
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getProject()
  },
  changeDate1(e) {
    this.setData({
      dataTime1: e.detail.value,
      dataTime: e.detail.value,
      dataFlag: false,
      dataColor: true
    })
    this.getProject()
  },
  changeDate2(e) {
    this.setData({
      dataTime2: e.detail.value,
      dataFlag: false,
      dataColor: true
    })
    this.getProject()
  },

  dataShow: function () {//日期下拉
    if (this.data.dataFlag){
      this.setData({
        dataFlag : !this.data.dataFlag,
        screenFlag: false,
        screenColor: false
      })
    }else{
      this.setData({
        dataFlag: !this.data.dataFlag,
        screenFlag: false,
        screenColor: false
      })
    }
  },

  screenShow: function () {//综合下拉
    if (this.data.screenFlag){
      this.setData({
        screenFlag : !this.data.screenFlag,
        dataFlag: false,
        dataColor: false
      })
    }else{
      this.setData({
        screenFlag: !this.data.screenFlag,
        dataFlag: false,
        dataColor: false
      })
    }
  },
  screenChoice:function(e){//综合下拉选择子项
    console.log(e)
    this.setData({
      screen: e.currentTarget.dataset.item,
      "screenDown.screen": e.currentTarget.dataset.index,
      screenFlag: false,
      screenColor:true
    })
    this.getProject()
  },
  handletouchtart: function (event) {//点击透明背景隐藏下拉
    this.setData({
      screenFlag: false,
      dataFlag: false
    })
  },
  
  // 统一请求
  getProject(e) {
    let that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.request({
      url: globalIp + 'home/ordr/showProjectForMonth',
      data: {
        sdate: that.data.dataTime1 == '日期选择' ? '' : that.data.dataTime1,
        openId: that.data.openId || wx.getStorageSync('userInfo').openid,
        login: wx.getStorageSync('userInfo').login || 'N'
      },
      header: {
        'content-type': 'application/json'
      },
      method: 'POST',
      success: function(res) {
        if (res.data.jsonObject) {
          var data = res.data.jsonObject.data;
          var arr = [];
          var res = [];
          for (var i in data) {
            arr[i] = [];
            for(var j in data[i]) {
              arr[i].push(data[i][j]);
            }
          }
          for (var i = 0; i < arr[0].length; ++i) {
            res[i] = [];
            for (var j = 0; j < arr.length; ++j) {
              res[i][j] = arr[j][i];
            }
          }
          // console.log(res)
          that.setData({
            projectList: res
          })
        } 

        /* 柱状图 */
        var xData = [];
        var sdData = [];
        var yzData = [];
        var chData = [];
        var length = data.length;
        for (var i = 0, j = 1; j < length - 1; i++, j++) {
          xData[i] = data[j].销售部门.substring(0,4);
          sdData[i] = data[j].收单量KG;
          yzData[i] = data[j].已转生产;
          chData[i] = data[j].出货KG;
        }
        barChart.setOption({
          legend: {
            data: ["收单量(t)", "已转生产(t)", "出货(t)"]
          },
          grid: {
            left: "5%",
            right: "5%",
            containLabel: true
          },
          xAxis: {
            data: xData,
            nameLocation: 'middle',
            fontWeight: 'bold'
          },
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: "收单量(t)",
              data: sdData,
              type: 'bar'
            },
            {
              name: "已转生产(t)",
              data: yzData,
              type: 'bar'
            },
            {
              name: "出货(t)",
              data: chData,
              type: 'bar'
            }
          ]
        });
        
      },
      fail: function(res) {
        wx.showToast({
          title: '请返回上一页面，重新请求',
          icon: 'none',
          duration: 2000,
        })
      },
      complete: function(res) {
        wx.hideLoading()
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})